package com.handinfo.android.uicontrols.controls;


import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

/**
 * 按钮控件
 * @author handinfo
 */
public class DWButton extends DWControl
{
	private Bitmap img_down = null;
	private boolean m_down = false;
	private int m_text_color = Tools.WHITE;
	private long m_failure_time = 0;//失效时间
	private long m_failure_start = 0;//失效时间开始的时间
	private DWFont m_font = DWFont.getDefaultFont();

	public DWButton(String text, Bitmap background)
	{
		this(text, background, 0, 0);
	}

	public DWButton(String text, Bitmap background, int x, int y)
	{
		if (background == null) {
			throw new NullPointerException("DWButton background is null!");
		}
		setPoint(x, y);
		setShowWideHigh(background.getWidth(), background.getHeight());
		img_background = background;
		setName(text);
	}

	public DWButton(String text, int w, int h)
	{
		this(text, 0, 0, w, h);
	}

	public DWButton(String text, int x, int y, int w, int h)
	{
		setName(text);
		setPoint(x, y);
		setShowWideHigh(w, h);
		setTouchZoomIn(10, 10);
	}
	/**
	 * 设置按下后的图片
	 * @param img
	 */
	public void setDownImage(Bitmap img) {
		img_down = img;
	}
	/**
	 * 设置按钮字颜色
	 * @param color
	 */
	public void setTextColor(int color) {
		m_text_color = color;
	}
	
	/**
	 * 设置按钮失效时间
	 * @param second
	 */
	public void setFailureTime(long second) {
		m_failure_time = second * 1000;
		m_failure_start = System.currentTimeMillis();
	}
	
//	public void setText(String text)
//	{
//		m_text = text;
//	}

	public void render(DWGraphics g)
	{
		g.setClip(m_show_x, m_show_y, m_show_w + 1, m_show_h + 1);
		if (img_background != null) {
			if ((m_down || m_failure_time > 0) && img_down != null) {
				g.drawBitmap(img_down, m_show_x, m_show_y, Tools.LEFT_TOP);
			} else {
				g.drawBitmap(img_background, m_show_x, m_show_y, Tools.LEFT_TOP);
			}
		}
		else {
			g.setColor(-1);
			g.fillRect(m_show_x, m_show_y, m_show_w, m_show_h);
		}
		if (m_name != null) {
			g.drawShadowString(m_font, m_name, DWControlsManager.COLOR_STROKE, m_text_color, m_show_x + (m_show_w >> 1),
					m_show_y + (m_show_h >> 1) - (m_font.getHeight() / 2), Tools.HCENTER_TOP);
		}
		renderRect(g);
		renderGuide(g);
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		setSubClip(g, x, y, w, h, offsetx, offsety);
		if (img_background != null) {
			if ((m_down || m_failure_time > 0) && img_down != null) {
				g.drawBitmap(img_down, x + m_show_x, y + m_show_y - offsety, Tools.LEFT_TOP);
			} else {
				g.drawBitmap(img_background, x + m_show_x, y + m_show_y - offsety, Tools.LEFT_TOP);
			}
		}
		else {
			g.setColor(-1);
			g.fillRect(x + m_show_x, y + m_show_y - offsety, m_show_w, m_show_h);
		}
		if (m_name != null) {
			g.drawShadowString(m_font, m_name, DWControlsManager.COLOR_STROKE, m_text_color, x + m_show_x + (m_show_w >> 1),
					y + m_show_y + (m_show_h >> 1) - (m_font.getHeight() / 2) - offsety, Tools.HCENTER_TOP);
		}
		renderLimitsRect(g, x, y, offsetx, offsety);
		renderGuide(g, x - offsetx, y - offsety);
	}

	public void logic()
	{
		if (m_failure_time > 0) {
			long currentTime = System.currentTimeMillis();
			if (currentTime - m_failure_start >= m_failure_time) {
				m_failure_time = 0;
				m_failure_start = 0;
			}
		}
	}
	
	public void refreshControl()
	{
	}
	
	public boolean doDown(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			m_down = true;
			return true;
		}
		return false;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (m_down) {
			m_down = false;
			if (checkPointOnRect(end.x, end.y)) {
				if (m_listener != null && m_failure_time <= 0) {
					m_listener.OnClick();
				}
				return true;
			}
		}
		return false;
	}
	
	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (m_down) {
			m_down = false;
			if (checkPointOnRect(end.x, end.y)) {
				if (m_listener != null && m_failure_time <= 0) {
					m_listener.OnClick();
				}
				return true;
			}
		}
		return false;
	}

	public boolean doClick(float x, float y)
	{
		if (m_down) {
			m_down = false;
		}
		if (checkPointOnRect(x, y)) {
			if (m_listener != null && m_failure_time <= 0) {
				m_listener.OnClick();
			}
			setGuide(false);
			return true;
		}
		return false;
	}

}
